package cn.kgc.musicserver.controller;

import cn.hutool.json.JSONUtil;
import cn.kgc.musicserver.domain.dto.Result;
import cn.kgc.musicserver.domain.dto.ResultUtil;
import cn.kgc.musicserver.domain.entity.Video;
import cn.kgc.musicserver.service.IVideoService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

@RestController
@RequiredArgsConstructor
@RequestMapping("/video")
public class VideoController {
    private final IVideoService videoService;

    /**
     * 分页视频
     * @param pageNo
     * @param size
     * @param search
     * @return
     */
    @GetMapping("page")
    public Result getVideoPage(
            @RequestParam(value = "pageNo", defaultValue = "1") int pageNo,
            @RequestParam(value = "size", defaultValue = "5") int size,
            @RequestParam(value = "search", required = false) String search) {
        System.out.println("pageNo---:" + pageNo);
        QueryWrapper<Video> wrapper = new QueryWrapper<>();
        wrapper.like(search != null, "v_name", search);
        wrapper.orderByAsc("id"); // 按 id 升序排序
        IPage<Video> page = new Page<>(pageNo, size);
        page = videoService.page(page, wrapper);
        return ResultUtil.returnDataSuccess(page);
    }

    /**
     * 视频详情
     * @param id
     * @return
     */
    @GetMapping("detail")
    public Result getVideoDetail(@RequestParam Integer id) {
        System.out.println("Fetching video details for ID: " + id); // 增加日志
        Video video = videoService.getById(id);
        if (video == null) {
            System.out.println("Video not found for ID: " + id); // 增加日志
            return ResultUtil.returnFail("视频不存在");
        }
        System.out.println("Video fetched successfully: " + video); // 增加日志
        return ResultUtil.returnDataSuccess(video);
    }

    /**
     * 修改视频   这还是一个注释
     * @param video
     * @return
     */
    @PostMapping("edit")
    public Result editVideo(@RequestBody Video video) {
        boolean b = videoService.updateById(video);
        if (!b) {
            return ResultUtil.returnFail("修改失败");
        }
        return ResultUtil.returnSuccess("修改成功");
    }

    /**
     * 添加视频  这是一个注释
     * @param video
     * @return
     */
    @PostMapping("add")
    public Result addVideo(@RequestBody Video video) {
        boolean b = videoService.save(video);
        if (!b) {
            return ResultUtil.returnFail("添加失败");
        }
        return ResultUtil.returnSuccess("添加成功");
    }

    /**
     * 删除视频
     * @param id
     * @return
     */
    @DeleteMapping("del")
    public Result delVideo(@RequestParam Integer id) {
        boolean b = videoService.removeById(id);
        if (!b) {
            return ResultUtil.returnFail("删除失败");
        }
        return ResultUtil.returnSuccess("删除成功");
    }

    /**
     * 更新播放量
     * @param video
     * @return
     */
    @PostMapping("update-Playback")
    public Result updatePlayback(@RequestBody Video video) {
        System.out.println("Received request to update playback count for video: " + video.getId()); // 增加日志
        if (video.getId() == null) {
            System.out.println("Video ID is null in the request body."); // 增加日志
            return ResultUtil.returnFail("视频ID不能为空");
        }

        Video existingVideo = videoService.getById(video.getId());
        if (existingVideo == null) {
            System.out.println("Video with ID " + video.getId() + " not found."); // 增加日志
            return ResultUtil.returnFail("视频不存在");
        }

        existingVideo.setVPlayback(existingVideo.getVPlayback() + 1);
        boolean b = videoService.updateById(existingVideo);
        if (!b) {
            System.out.println("Failed to update playback count for video ID: " + video.getId()); // 增加日志
            return ResultUtil.returnFail("更新失败");
        }
        System.out.println("Successfully updated playback count for video ID: " + video.getId()); // 增加日志
        return ResultUtil.returnSuccess("更新成功");
    }




}